package com.wc.service.impl;

import com.wc.dao.UserDao;
import com.wc.dao.bean.PageBean;
import com.wc.dao.impl.UserDaoImpl;
import com.wc.domain.User;
import com.wc.service.UserService;
import com.wc.utils.JdbcUtil;
import com.wc.vo.UserVO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author hc
 * @date 2022/4/9 17:19
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public int getCount() throws SQLException {
        int count = userDao.selectCount();
        return count;
    }

    @Override
    public PageBean<User> getPage(Integer pageNum, Integer pageSize) throws SQLException {
        int count = userDao.selectCount();
        List<User> users = userDao.selectPage(pageNum, pageSize);
        PageBean<User> pageBean = new PageBean(pageNum, pageSize, count, users);
        return pageBean;
    }

    @Override
    public boolean add(User user) throws SQLException {
        //先判断待添加的user的tel是否存在，如果存在，返回false
        int telExists = userDao.isTelExists(user.getTel());
        if (telExists >0) {
            return false;
        }
        //如果不存在，再添加，成功返回true，失败返回false
        int insert = userDao.insert(user);
        return insert == 1 ? true : false;
    }

    @Override
    public boolean isTelExists(String tel) throws SQLException {
        int telRes = userDao.isTelExists(tel);

        return telRes>0?true:false;
    }

}
